//
//  MDHWKWebViewManager.swift
//  JSTech
//
//  Created by 高得华 on 2019/5/28.
//  Copyright © 2020 华&梅. All rights reserved.
//

import UIKit

///协议地址 URL Schemes
let kMDHPTURLSchemes = "mJSTech://webview?"

///H5管理器的对象
///通知
public let kMDHWKWebViewManagerNotification = "kMDHWKWebViewManagerNotification"


/*
 1.系统消息：
 mJSTech://webview?{"activity":"systemMsg"}
 2.陌生私聊
 mJSTech://webview?{"activity":"chatPage"}
 3.动态消息
 mJSTech://webview?{"activity":"recordMsg"}
 */


/// - ===== H5管理器 ===== -
open class MDHWKWebViewManager: NSObject {
    
    ///下一步的操作
    public var blockPushNextVc: ((_ model: MDHWKWebViewModel)->())?
    
    ///拦截执行网页中的JS方法
    @objc public var blockPushScriptNextVc: ((_ userContentController: WKUserContentController, _ message: WKScriptMessage, _ webView: WKWebView?)->())?
    
    ///单例
    @objc public static let share = MDHWKWebViewManager.shared
    //在方法内定义静态变量
    @objc public static var shared: MDHWKWebViewManager {
        struct Static {
            static let instance: MDHWKWebViewManager = MDHWKWebViewManager()
        }
        return Static.instance
    }
    private override init() {
        super.init()
    }
    
    ///是否是喵喵玩协议
    @objc open func isMiaomiao(url: String) -> Bool {
        #if DEBUG
            print(url)
        #endif
        return url.hasPrefix(kMDHPTURLSchemes)
    }
    
    ///是否需要进行跳转
    @objc open func push(url: String) -> Bool {
        if url.count <= 0 {
            return false
        }
        var isOk = self.isMiaomiao(url: url)
        if isOk {
            let urlRange = url as NSString
            let urls = urlRange.replacingOccurrences(of: kMDHPTURLSchemes, with: "")
            if urls.count > 0 {
                let object = (urls as NSString).mj_JSONObject()
                if object != nil && ((object is NSDictionary) || (object is NSArray)) {
                    let model = MDHWKWebViewModel.mj_object(withKeyValues: object) ?? MDHWKWebViewModel()
                    if model.activity.count > 0 {
                        self.pushVc(model: model)
                    }else{
                        isOk = false
                    }
                }else{
                    isOk = false
                }
            }else{
                isOk = false
            }
        }
        isOk = url.hasPrefix("http")
        return isOk
    }
    
    
    ///进行页面的跳转 及其他的操作
    @objc open func pushVc(model: MDHWKWebViewModel) -> () {
        if blockPushNextVc != nil {
            blockPushNextVc!(model)
        }
        //NotificationCenter.default.post(name: NSNotification.Name.init(rawValue: kMDHWKWebViewManagerNotification), object: model)
        //NotificationCenter.default.post(name: NSNotification.Name.init(rawValue: kMDHWKWebViewManagerNotification), object: self, userInfo: ["data" : model])
    }
}


/// - ==== JS 与webView的交互 ==== -
open class MDHWKWebViewModel: NSObject {
    ///页面类型
    @objc public var activity: String = ""
    //index 默认选中第几个
    @objc public var index = 0
    
    ///h5界面的 SVGA 座驾 播放
    ///座驾图片名称 alias_name
    @objc var alias_name = ""
    ///座驾名称
    @objc var driver_name = ""

    /// - ===== js交互 下载图片 ==== -
    @objc public var pic_url: String = ""
    
    ///分享
    ///title 标题
    @objc var title = ""
    ///content 内容
    @objc var content = ""
    ///image 图片
    @objc var image = ""
    ///url 链接
    @objc var url = ""
    ///share_type
    @objc var share_type = ""
    
    ///提现时的钱
    //money
    @objc public var money = ""
    //type
    @objc public var type = ""
    
    //用户的ID
    @objc public var user_id = 0
}
